package com.summer.shopping.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.summer.shopping.domain.common.req.ReqHolder;
import com.summer.shopping.domain.table.Address;
import com.summer.shopping.domain.table.dto.PageDTO;

import com.summer.shopping.mapper.AddressMapper;
import com.summer.shopping.service.AddressService;
import com.summer.shopping.utils.PageResult;
import org.springframework.stereotype.Service;


@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements AddressService {

    @Override
    public PageResult<Address> pageQuery(PageDTO pageDTO) {
        Page<Address> page = new Page<>(pageDTO.getPageNo(), pageDTO.getPageSize());

        QueryWrapper<Address> wrapper = new QueryWrapper<>();
        OrderItem orderItem = new OrderItem().setAsc(false).setColumn("create_time");
        page.addOrder(orderItem);
        //获取登录用户id
        Long userId = ReqHolder.getUserId();

        wrapper.eq("user_id",userId);

        Page<Address> resultPage = page(page, wrapper);
        PageResult pageResult = new PageResult<Address>();
        pageResult.setRecords(resultPage.getRecords());
        pageResult.setTotal(resultPage.getTotal());
        return pageResult;
    }

}
